package com.jialanmo.zhihudaily.ui.news;

import com.jialanmo.zhihudaily.Application;
import com.jialanmo.zhihudaily.data.model.News;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public final class NewsPresenter implements NewsContract.Presenter {

    private final NewsContract.View mView;
    private final int mId;

    public NewsPresenter(NewsContract.View view, int id) {
        mView = view;
        mId = id;
    }

    @Override
    public void start() {
        getNews();
    }

    @Override
    public void getNews() {
        Application.getApplication(mView.getContext()).getZhihuApi()
                .getNews(mId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<News>() {
                    @Override
                    public void call(News news) {
                        mView.showNews(news);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showMessage(throwable.getMessage());
                    }
                });
    }
}
